package com.atguigu.eduorder.service.impl;

import com.atguigu.eduorder.utils.OrderNoUtil;
import com.atguigu.commonutils.vo.OrderCourseWebVo;
import com.atguigu.commonutils.vo.OrderUcenterVo;
import com.atguigu.eduorder.entity.TOrder;
import com.atguigu.eduorder.mapper.TOrderMapper;
import com.atguigu.eduorder.service.TOrderService;
import com.atguigu.eduorder.service.client.EduClient;
import com.atguigu.eduorder.service.client.UcenterClient;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author hwt
 * @since 2022-10-02
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Resource
    EduClient eduClient;

    @Resource
    UcenterClient ucenterClient;


    @Override
    public String createOrder(String courseId, String memberId) {
        // 通过远程调用，获取到课程相关信息
        OrderCourseWebVo courseById = eduClient.getCourseById(courseId);

        // 通过远程调用，获取到用户相关信息
        OrderUcenterVo userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        // 创建订单，设置对应的值
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());     // 设置订单号
        order.setCourseId(courseId);                    // 设置课程 id

        order.setCourseTitle(courseById.getTitle());    // 设置课程标题
        order.setCourseCover(courseById.getCover());    // 设置课程封面
        order.setTeacherName(courseById.getTeacherName());  // 设置课程讲师名称
        order.setTotalFee(courseById.getPrice());       // 设置课程价格
        order.setMemberId(memberId);          // 设置用户 id
        order.setMobile(userInfoOrder.getMobile());     // 设置用户手机号码
        order.setNickname(userInfoOrder.getNickname()); // 设置用户昵称
        order.setStatus(0);                             // 订单状态 (0:未支付，1:已支付)
        order.setPayType(1);                            // 支付类型 (0:支付宝，1微信)
        baseMapper.insert(order);

        //返回订单号
        return order.getOrderNo();
    }
}
